package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.parser.Feature
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 工单查询API
 */

public class apiGetWOHis implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetWOHis.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        /*
        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(callJson, Feature.OrderedField));
        log.info("[签名验证结果]:");

        log.info(signJson);
        if (signJson != null) {
            return signJson.toString();
        } else {
		*/
        try {
            //解析Json
            JSONObject inJson = JSONObject.parseObject(callJson, Feature.OrderedField);

            //获取查询参数
            String s_workorder_guid = inJson.getJSONObject("data").getString("s_workorder_guid");// 输入的工单ID

            log.debug("请求参数-查询条件-s_workorder_guid:" + s_workorder_guid);

            String q_WhereSQL = " Where 0 = 0";
            log.debug("初始化控制查询条件SQL:" + q_WhereSQL);

            // 模糊查询条件处理
            if (!KHUtil.isNull(s_workorder_guid)) {
                q_WhereSQL = q_WhereSQL + " And a.Workorder_Guid = '" + s_workorder_guid + "'";
            }

            q_WhereSQL = q_WhereSQL + " Order By Date_Format(a.modifieddate, '%Y-%m-%d %H:%i:%S') asc";
            log.debug("控制查询条件完成SQL:" + q_WhereSQL);

            String queryWorkOrderSQL = "select distinct t.modifieddate,t.workorderstepstatus,t.old_rolegroupdescription,t.old_staffname,t.rolegroupdescription,t.staffname,t.Old_Nodemapname,t.nodemapname,t.comments,t.comments_r_path,t.i from (" +
                    " Select Date_Format(a.modifieddate, '%Y-%m-%d %H:%i:%S') As modifieddate,\n" +
                    "                a.workorderstepstatus,\n" +
                    "                R1.Rolegroupdescription As old_rolegroupdescription,\n" +
                    "                S1.staffname            As old_staffname,\n" +
                    "                R2.rolegroupdescription,\n" +
                    "                S2.staffname,\n" +
                    "                Wnm1.Nodemapname        As Old_Nodemapname,\n" +
                    "                Wnm2.nodemapname,\n" +
                    "                a.comments,\n" +
                    "                a.comments_r_path,(@i:=@i+1) i\n" +
                    "  From Workorderstep3 a left join objective o on a.WORKORDER_GUID=o.OBJECTIVE_GUID\n" +
                    "  Left Join Wo3_Node_Map Wnm1 \n" +
                    "    On a.From_Stepidmap = Wnm1.Nodemap_Id and wnm1.map_id=o.MAP_ID\n" +
                    "  Left Join Wo3_Node_Map Wnm2\n" +
                    "    On a.To_Stepidmap = Wnm2.Nodemap_Id and wnm2.map_id=o.MAP_ID\n" +
                    "  Left Join Rolegroup R1\n" +
                    "    On a.Oldhandlegroup_Id = R1.Rolegroup_Id\n" +
                    "  Left Join Rolegroup R2\n" +
                    "    On a.Handlegroup_Id = R2.Rolegroup_Id\n" +
                    "  Left Join Staff S1\n" +
                    "    On a.Oldhandleby_Id = S1.Staff_Id\n" +
                    "  Left Join Staff S2\n" +
                    "    On a.Handleby_Id = S2.Staff_Id,(SELECT @i:=0) as i\n" + q_WhereSQL+") t order by t.i desc ";
            log.debug("执行工单查询数据SQL:" + queryWorkOrderSQL);

            List WorkOrderList = eliteBean.chaxun_sql(queryWorkOrderSQL, "dataSource");
            //获取总行数
            log.debug("执行工单查询数据SQL获取总条数:" + WorkOrderList.size());

            String reJoson = Util.getJsonFromList(WorkOrderList);
            reJoson=!KHUtil.isNull(reJoson)?reJoson:"[]";
            long endTime = System.currentTimeMillis();
            result.put("code", 1);
            result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
            JSONArray dataArr = JSONArray.parseArray(reJoson);
            result.put("data", dataArr);

        } catch (Exception e) {
            result.put("code", 1008);
            result.put("msg", e.getMessage());
        }

        //  }

        return result.toString();
    }
}